package cn.jeeweb.web.modules.sys.controller;

import cn.jeeweb.common.http.Response;
import cn.jeeweb.common.mvc.annotation.ViewPrefix;
import cn.jeeweb.common.mvc.controller.BaseBeanController;
import cn.jeeweb.common.mybatis.mvc.wrapper.EntityWrapper;
import cn.jeeweb.common.utils.FastJsonUtils;
import cn.jeeweb.common.utils.StringUtils;
import cn.jeeweb.web.aspectj.annotation.Log;
import cn.jeeweb.web.modules.sys.entity.*;
import cn.jeeweb.web.modules.sys.service.*;
import cn.xjzn.framework_Core.ModuleActionType;
import cn.xjzn.framework_Core.ModuleServe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


/**
 *
 */
@RestController
@RequestMapping("/ui/actionbutton")
@ViewPrefix("modules/UipackageInfo")
//@RequiresPathPermission("ui:packageinfo")
@Log(title = "ui_action_button")
public class UiActionButtonController extends BaseBeanController<UiActionButton> {
    @Autowired
    private IUiActionButtonService uiActionButtonService;
    @Autowired
    private IUiPackageInfoService uiPackageInfoService;


    @PostMapping("add")
    @Log(logType = ModuleActionType.INSERT)
//	@RequiresMethodPermissions("add")
    public Response add(UiActionButton entity, BindingResult result,
                        HttpServletRequest request, HttpServletResponse response) {
        // 验证错误
        this.checkError(entity, result);
        uiActionButtonService.insert(entity);
        return Response.ok("添加成功");
    }

    @PostMapping("{id}/update")
    @Log(logType = ModuleActionType.UPDATE)
//	@RequiresMethodPermissions("update")
    public Response update(UiActionButton entity, BindingResult result,
                           HttpServletRequest request, HttpServletResponse response) {
        // 验证错误
        this.checkError(entity, result);
        uiActionButtonService.insertOrUpdate(entity);
        return Response.ok("更新成功");
    }

    @PostMapping("{id}/delete")
    @Log(logType = ModuleActionType.DELETE)
//	@RequiresMethodPermissions("delete")
    public Response delete(@PathVariable("id") String id) {
        uiActionButtonService.deleteById(id);
        return Response.ok("删除成功");
    }

    /**
     * @param uiActionButton
     * @throws IOException
     */
    @RequestMapping(value = "getList", method = {RequestMethod.GET, RequestMethod.POST})
    @Log(logType = ModuleActionType.SELECT)
//	@RequiresMethodPermissions("list")
    public void ajaxList(UiActionButton uiActionButton) throws IOException {
        List<UiActionButton> uiActionButtons = uiActionButtonService.getList(uiActionButton);
        FastJsonUtils.print(uiActionButtons);
    }

    @RequestMapping(value = "getInitUiActionButton", method = {RequestMethod.GET, RequestMethod.POST})
    @Log(logType = ModuleActionType.SELECT)
    public void getInitUiActionButton(String moduleId) {
        List<UiActionButton> uiActionButtons = getUiActionButton(moduleId);
        FastJsonUtils.print(uiActionButtons);
    }

    @RequestMapping(value = "saveUiActionButton", method = {RequestMethod.GET, RequestMethod.POST})
    @Log(logType = ModuleActionType.INSERT)
    public Response saveUiActionButton(String packageId) {
        String moduleId = "";
        UiPackageInfo search = new UiPackageInfo();
        search.setId(packageId);
        UiPackageInfo result = uiPackageInfoService.getUiPackageInfo(search);
        if (result != null && StringUtils.isNotBlank(result.getId())) {
            moduleId = result.getModuleId();
        }
        List<UiActionButton> uiActionButtons = getUiActionButton(moduleId);
        EntityWrapper<UiActionButton> entityWrapper = new EntityWrapper<UiActionButton>(entityClass);
        entityWrapper.eq("packageId", packageId);
        uiActionButtonService.delete(entityWrapper);
        if (uiActionButtons.size() > 0) {
            for (UiActionButton uiActionButton : uiActionButtons) {
                uiActionButton.setPackageId(packageId);
                uiActionButtonService.insert(uiActionButton);
            }
        }
        return Response.ok("添加成功");
    }

    private List<UiActionButton> getUiActionButton(String moduleId) {
        List<UiActionButton> uiActionButtons = new ArrayList<UiActionButton>();
        ModuleServe moduleServe = ModuleServe.getEnumByValue(moduleId);
        if (!"-1".equals(moduleServe.getValue())) {
            ModuleActionType[] moduleActionTypes = moduleServe.getActions();
            if (moduleActionTypes != null && moduleActionTypes.length > 0) {
                for (ModuleActionType moduleActionType : moduleActionTypes) {
                    if (!ModuleActionType.SELECT.name().equals(moduleActionType.name())) {
                        UiActionButton uiActionButton = new UiActionButton();
                        uiActionButton.setText(moduleActionType.getName());
                        uiActionButton.setCode(moduleActionType.toString());
                        uiActionButton.setModuleId(moduleId);
                        uiActionButton.setSort(moduleActionType.getSort());
                        uiActionButtons.add(uiActionButton);
                    }
                }
            }
        }
        uiActionButtons.sort(Comparator.comparing(UiActionButton::getSort));
        return uiActionButtons;
    }
}